window.onload = function () {
  axios.defaults.baseURL = "http://124.223.14.236:3001/api"
  // 1. 定义数据
  // 列表数据
  let token = null
  //获取token值
  async function denglu() {
    const data = await axios.post('/user/login', {
      username: "tv",
      password: "123456"
    })
    token = data.data.data.token
    //获取购物车数据
    const { data: { data: arr } } = await axios.get('/cart', {
      headers: {
        Authorization: token
      }
    })


    //渲染数据 
    const trs = arr.map((item, i) => {
      return `
<tr  align ="center">
<td>
  <input class="check" type="checkbox">
</td>
<td>${i + 1}</td >
<td>${item.title}</td>
<td class="img">
  <img src=${item.img} style="width:70px;height:70px;" alt="">
</td>
<td class="price">${(item.price).toFixed(2)}</td>
<td class="num">
  <span class="reduce">-</span>
  <input value="${item.buy_num}" type="text">
  <span class="add">+</span>
</td>
<td class="account">${(item.buy_num * item.price).toFixed(2)}</td>
<td>
  <button class="del" data-id='${item.id}'>删除</button>
</td>
</tr >
    `
    }).join('')
    document.querySelector('tbody').innerHTML = trs


    //删除数据
    document.querySelector('tbody').addEventListener('click', async e => {
      if (e.target.tagName === 'BUTTON') {
        let ids = JSON.parse(e.target.dataset.id)
        let resData = await axios.delete(`/cart/delete/${ids}`, {
          headers: {
            Authorization: token
          }
        })
        if (resData.status !== 200) return alert(resData.statusText)
        alert(resData.data.msg)
        denglu()
      }


    })


    //修改数据

    let inputs = document.querySelectorAll('.check')
    for (let i = 0; i < inputs.length; i++) {
      inputs[i].addEventListener('change', function () {
        for (let j = 0; j < inputs.length; j++) {
          if (inputs[j].checked) {

            console.log(j);

          }
        }


      })

    }








  } denglu()





  //总价和数量


}






